package com.vip.vcsp.multiprocess;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;

import java.lang.reflect.Method;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MainProcessService extends Service {

    @SuppressLint("HandlerLeak")
    private Messenger messenger = new Messenger(new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Log.d("MainProcessService","uid:"+msg.sendingUid);
            }
            if (msg.what == MultiProcessManager.MSG_INVOKE_MSG) {
                Bundle bundleReq = msg.getData();
                if (bundleReq != null) {
                    try {
                        Class<?>[] parameterTypes = (Class<?>[]) bundleReq.getSerializable("parameterTypes");
                        Object[] arguments = (Object[]) bundleReq.getSerializable("arguments");
                        String serviceName = bundleReq.getString("serviceName");
                        String methodName = bundleReq.getString("methodName");

                        if (TextUtils.isEmpty(methodName) || "toString".equals(methodName)) {
                            return;
                        }

                        Object serviceObj = MultiProcessManager.getService(serviceName);
                        if (serviceObj == null) {
                            Log.e("rpc", serviceName + " not found");
                            return;
                        }
                        Method method = serviceObj.getClass().getMethod(methodName, parameterTypes);
                        Object result = method.invoke(serviceObj, arguments);

                        //发送返回结果到客户端
                        Messenger clientMessenger = msg.replyTo;
                        Message message = Message.obtain();
                        message.what = MultiProcessManager.MSG_INVOKE_MSG;
                        Gson gson = new Gson();
                        Bundle bundleRet = msg.getData();
                        bundleRet.putSerializable("resultType", method.getReturnType());
                        bundleRet.putString("resultData", gson.toJson(result));
                        bundleRet.putString("methodName", methodName);
                        message.setData(bundleRet);
                        clientMessenger.send(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return messenger.getBinder();
    }
}
